/**
 * 根据归一化的数据,生成颜色
 * @param data 数据
 * @param colormap 颜色表
 * @returns {
 *   colors: number[], // 拉平的颜色数组
 *   itemSize: number, // 每项颜色的长度
 * }
 */
export function getColorByData(
  data: number[],
  colormap: number[][],
): {
  colors: number[];
  itemSize: number;
} {
  const colormapLength = colormap.length;
  const itemSize = colormap[0].length;
  const colors = data.flatMap((value: number) => {
    let index = Math.round(value * colormapLength);
    index = Math.max(0, Math.min(index, colormapLength - 1));
    return colormap[index];
  });
  return {
    colors,
    itemSize,
  };
}
